package com.yangwudi.interceptor;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

//<----------------------------------该部分为练习内容，与项目无关，可直接删除----------------------------------->

@Slf4j
@Component
public class DemoInterceptor implements HandlerInterceptor {
    //在目标资源方法（Controller）运行之前运行 -返回值:true 放行，false 不放行
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.info("preHandle ....");
        return true;
    }

    //在目标资源方法运行之后运行
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        log.info("postHandle ....");
    }

    //视图渲染完毕后运行
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        log.info("afterCompletion ....");
    }
}


//拦截器Interceptor的使用步骤
//定义:实现HandlerInterceptor接口
//1. preHandle:在目标资源方法（Controller）运行之前运行 -返回值:true 放行，false 不放行
//2. postHandle:在目标资源方法运行之后运行
//3. afterCompletion:视图渲染完毕后运行
//配置:定义一个配置类实现WEbMvcConfigurer接口，注册拦截器("/**")